﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Browser
{
    public partial class Upload : System.Web.UI.Page
    {
        protected string userName = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["loggedin"] == null || (bool)Session["loggedin"] != true)
                Response.Redirect("login.aspx?login=login");

            userName = (string)Session["username"];
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["fileUpdated"] = false;

            if (FileUpload1.HasFile)
            {
                string fileExt =System.IO.Path.GetExtension(FileUpload1.FileName);

                //string distination = @"C:\Documents and Settings\khedidja.aoukli\Mes documents\Browser-08-2011\Browser-28-04\Browser\Browser\Dico\";
                string destination = Server.MapPath(".") + @"\xml\" + userName + @"\dico\";

                if (!Directory.Exists(Server.MapPath(".") + @"\xml\" + userName + @"\dico\"))
                    Directory.CreateDirectory(destination);

                if (fileExt == ".xml")
                {
                    try
                    {
                        FileUpload1.SaveAs(destination+FileUpload1.FileName);

                        Label1.Text = "File name: " +
                            FileUpload1.PostedFile.FileName + "<br>" +
                            FileUpload1.PostedFile.ContentLength + " kb<br>" +
                            "Content type: " +
                            FileUpload1.PostedFile.ContentType
                            + "Location where saved: " +
                             destination + "\\" +
                             System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName) + "";

                        Session["fileUpdated"] = true;

                        Response.Redirect("polka.aspx");
                    }
                    catch (Exception ex)
                    {
                        Label1.Text = "ERROR: " + ex.Message.ToString();
                    }
                }
                else
                {
                    Label1.Text = "Only .xml files allowed!";
                }
            }
            else
            {
                Label1.Text = "You have not specified a file.";
            }
        }
       
    }
}